import java.util.HashMap;
// 存在重复元素 II
//测试链接 https://leetcode.cn/problems/contains-duplicate-ii/
public class ContainsNearbyDuplicate {
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        HashMap<Integer,Integer> hash = new HashMap<>();

        for(int i = 0; i < nums.length; i++){
            if(hash.containsKey(nums[i])){
                int abs = i - hash.get(nums[i]);
                if(abs <= k){
                    return true;
                }
            }
            hash.put(nums[i],i);
        }
        return false;
    }
}
